<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          textStyle: {
            fontWeight: 'bold',
            fontSize: 18, // 字体大小
            color: '#ffffff' // 字体颜色
          },
          text: '居家服务区域分布',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          textStyle: {
            fontSize: 14, // 字体大小
            color: '#ffffff' // 字体颜色
          },
          left: 'left',
          orient: 'vertical'
        },
        calculable: true,
        series: [
          {
            name: '居家服务区域分布',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 7264, name: '南关区' },
              { value: 7983, name: '二道区' }
            ]
          }
        ]
      })
    }
  }
}
</script>
